package com.sense.kukoo.common.wiget.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.PopupWindow
import com.sense.kukoo.common.R
import com.sense.kukoo.common.utils.ApplicationContext

class DeletePopupWindow: PopupWindow {

    private val TAG: String = javaClass.simpleName
    private var listener: (()->Unit)? = null

    constructor(contentView: View, width: Int, height: Int, focusable: Boolean): super(contentView, width, height, focusable)

    init {
        setTouchable(true)
        setOutsideTouchable(true)
        setElevation(4f)
        //
        contentView.setOnClickListener {
            dismiss()
            listener?.invoke()
        }
    }

    companion object{

        fun autoShow(context: Context, anchor: View, isUp: Boolean, listener: ()->Unit): DeletePopupWindow{
            val inflate: View = LayoutInflater.from(context).inflate(R.layout.common_popup_delete, null, false)
            val width = ApplicationContext.dp2px(142f)
            val height = ApplicationContext.dp2px(44f)
            //
            val popup = DeletePopupWindow(inflate, width, height, true)
            popup.listener = listener
            //
            val xoff = 0
            var yoff = 0
            if (isUp) {
                yoff -= popup.getHeight() + anchor.height + ApplicationContext.dp2px(7f)
            } else {
                yoff = ApplicationContext.dp2px(7f)
            }
            popup.showAsDropDown(anchor, xoff, yoff, Gravity.BOTTOM or Gravity.END)
            return popup
        }
    }

}